$(function(){
  $("#login1").click(function(){
    location.href = 'login';
  });
  $("#register1").click(function(){
    var username = $("#username").val();
    var password = $("#password").val();
    var password1 = $("#password1").val();
    var regiscode = $("#regiscode").val();
    if(password !== password1){
      $("#password").css("border","1px solid red");
      $("#password1").css("border","1px solid red");
    }else if(password === password1){
      var data = {"uname":username,"upwd":password};
      $.ajax({
        url: '/register',
        type: 'post',
        data: data,
        success: function(data,status){
          if(status == 'success'){
            location.href = 'login';
          }
        },
        error: function(data,err){
          location.href = 'register';
        }
      });
    }
  });
});

function register_user(){
  var name = $("#username").val();
  var realname = $("#realname").val();
  var password = $("#password").val();
  var regiscode = $("#regiscode").val();
  var position = $("#position").val();
  var phone = $("#phone").val();
  var email = $("#email").val();
  var data = {
    "name": name,
    "realname": realname,
    "password": password,
    "phone": phone,
    "email": email,
    "position": position,
    "regcode": regiscode
  };
  $.ajax({
    url: '/reg_person',
    type: 'post',
    data: data,
    success: function(data,status){
      if(status == 'success'){
        //location.href = 'login';
        console.log(data);
      }
    },
    error: function(data,err){
      //location.href = 'reg_company';
      console.log(data);
    }
  });
}

function check_user(){
  var name = $("#username").val();
  var data = {
    "name": name
  };
  $.ajax({
    url: '/check_user',
    type: 'post',
    data: data,
    success: function(data,status){
      if(status == 'success'){
        //location.href = 'login';
        console.log(data);
      }
    },
    error: function(data,err){
      //location.href = 'reg_company';
      console.log(data);
    }
  });
}

function check_regcode(){
  var regcode = $("#regiscode").val();
  var data = {
    "regcode": regcode
  };
  $.ajax({
    url: '/check_regcode',
    type: 'post',
    data: data,
    success: function(data,status){
      if(status == 'success'){
        //location.href = 'login';
        console.log(data);
      }
    },
    error: function(data,err){
      //location.href = 'reg_company';
      console.log(data);
    }
  });
}

function check_code(code){
  var data = {
    "code": code
  };
  $.ajax({
    url: '/ccap_check',
    type: 'post',
    data: data,
    success: function(data,status){
      if(status == 'success'){
        //location.href = 'login';
        console.log(data);
      }
    },
    error: function(data,err){
      //location.href = 'reg_company';
      console.log(data);
    }
  });
}

function get_ccap(){
  $.ajax({
    url: '/ccap',
    type: 'get',
    success: function(data,status){
      $('#cap_show').html(data);
    },
    error: function(data,err){
      console.log(data);
    }
  });
}
